<?php

declare(strict_types=1);

namespace Shopper\Core\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Shopper\Core\Database\Factories\CountryFactory;
use Shopper\Core\Models\Traits\HasZones;

/**
 * @property-read int $id
 * @property-read string $name
 * @property-read string $name_official
 * @property-read string $region
 * @property-read string $subregion
 * @property-read string $cca3
 * @property-read string $cca2
 * @property-read string $flag
 * @property-read string $svg_flag
 * @property-read float $latitude
 * @property-read float $longitude
 * @property-read array<array-key, mixed> $phone_calling_code
 * @property-read array<array-key, mixed> $currencies
 */
class Country extends Model
{
    /** @use HasFactory<CountryFactory> */
    use HasFactory;

    use HasZones;

    public $timestamps = false;

    protected $guarded = [];

    public function getTable(): string
    {
        return shopper_table('countries');
    }

    protected static function newFactory(): CountryFactory
    {
        return CountryFactory::new();
    }

    protected function casts(): array
    {
        return [
            'phone_calling_code' => 'array',
            'currencies' => 'array',
        ];
    }

    protected function svgFlag(): Attribute
    {
        return Attribute::get(
            fn (): string => url(shopper()->prefix().'/images/flags/'.mb_strtolower($this->cca2).'.svg')
        );
    }
}
